<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderIndex">
              <a-input v-model="model.orderIndex"placeholder="请输入序号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <j-popup
                v-model="model.name"
                field="name"
                org-fields="name,dept,difficult_level,labour_cost_inward,labour_cost_outward,attrition_rate,man_hour,requirement,labour_cost_outwork,labour_unit_cost,remark"
                dest-fields="name,dept,difficultLevel,labourCostInward,labourCostOutward,attritionRate,manHour,requirement,labourCostOutwork,labourUnitCost,remark"
                code="working_procedure_info_report"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="执行部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dept">
              <j-select-depart v-model="model.dept"multi />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="难度级别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="difficultLevel">
              <a-input v-model="model.difficultLevel"placeholder="请输入难度级别" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="对内工价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourCostInward">
              <a-input v-model="model.labourCostInward"placeholder="请输入对内工价" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="对外工价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourCostOutward">
              <a-input v-model="model.labourCostOutward"placeholder="请输入对外工价" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="耗率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attritionRate">
              <a-input v-model="model.attritionRate"placeholder="请输入耗率" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manHour">
              <a-input v-model="model.manHour"placeholder="请输入工时" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="要求" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="requirement">
              <a-input v-model="model.requirement"placeholder="请输入要求" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="外发工价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourCostOutwork">
              <a-input v-model="model.labourCostOutwork"placeholder="请输入外发工价" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单件工费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourUnitCost">
              <a-input v-model="model.labourUnitCost"placeholder="请输入单件工费" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark"placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "StyleWorkingProcedureModal",
    components: {
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        title:"操作",
        width:800,
        visible: false,
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/styleInfo/styleInfo/addStyleWorkingProcedure",
          edit: "/styleInfo/styleInfo/editStyleWorkingProcedure",
        }

      }
    },
    created () {
    //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model['styleInfoId'] = this.mainId
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(value,row){
       this.model = Object.assign(this.model, row);
      },


    }
  }
</script>
